//
//  EMSAppViewController.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 6/18/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "EMSAppViewController.h"
#import "EMSAppController.h"
#import "EMSCharactersWindowController.h"
#import "EMSCharacterViewController.h"
#import "EMSCharacterDataController.h"
#import "EMSLearnedSkillFieldCell.h"
#import "EMSGeneralUtils.h"
#import "EMSSkillTrainingInfo.h"
#import "EVEAPICoreDataController.h"
#import "EVECDLearnedSkill.h"
#import "EVECDSkill.h"
#import "EVECDCharacter.h"
#import "EVECDQueueItem.h"

@implementation EMSAppViewController


-(void)awakeFromNib
{
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(characterQueueUpdateDidFinish:) 
												 name:CHARACTER_SKILL_QUEUE_API_TYPE 
											   object:nil];
	[progressBar bind:@"contentSet"
			 toObject:queuedSkillsArrayController
		  withKeyPath:@"arrangedObjects"
			  options:nil];
}

-(IBAction)removeCharacter:(id)sender
{	
	[appController removeCharacter];
}


-(IBAction)updateCharacter:(id)sender
{
	//[updateIndicator setUsesThreadedAnimation:YES];
	//[updateIndicator startAnimation:nil];
	if(appController.selectedCharacterController)
	{
		[appController.selectedCharacterController performUpdate];
	}
}

-(IBAction)launchSkillPlanner:(id)sender
{
	if(appController.selectedCharacterController)
	{
		[appController.selectedCharacterController launchSkillPlanner];
	}
}

-(IBAction)resetCache:(id)sender
{
	NSAlert* alert = [NSAlert alertWithMessageText:@"Reset Data" 
									 defaultButton:@"OK" 
								   alternateButton:@"Cancel"
									   otherButton:nil 
						 informativeTextWithFormat:@"Are you sure you want to reset the cache?"];
	int response = [alert runModal];
	if(response = NSAlertDefaultReturn)
	{
		[appController resetAndRestart];
	}
}

-(void)characterQueueUpdateDidFinish:(NSNotification*)notification
{
	/*NSString* notificationCharName = [[notification userInfo] objectForKey:CHAR_INFO_DICT_NAME];
	if([notificationCharName isEqualToString:[appController.selectedCharacterController charName]])
	{
		[updateIndicator stopAnimation:nil];
	}*/
}

-(NSString *)outlineView:(NSOutlineView *)ov 
		  toolTipForCell:(NSCell *)cell 
					rect:(NSRectPointer)rect 
			 tableColumn:(NSTableColumn *)tc 
					item:(id)item 
		   mouseLocation:(NSPoint)mouseLocation
{
	NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
	[numberFormatter setFormat:@"###,###,###,###"];
	if([[[[item representedObject] entity] name] isEqualToString:@"EVECDLearnedSkill"])
	{
		EVECDLearnedSkill* learnedSkill = [item representedObject];
		NSString* skillName = learnedSkill.skill.name;
		int level = learnedSkill.level.intValue;
		
		//Skill Description String.
		NSString* skillDescription = learnedSkill.skill.skillDescription;
		
		//Calculated time to reach next level.
		NSTimeInterval totalSecondsToNextLevel = [EMSSkillTrainingInfo trainingTimeToNextLevelForLearnedSkill:learnedSkill];
		
		HumanNotationTime time = [EMSSkillTrainingInfo humanNotationTimeFromTimeInterval:totalSecondsToNextLevel];
		NSString* timeToTrainString;
		if(level == 5)
		{
			timeToTrainString = @"";
		} else {
			timeToTrainString = [NSString stringWithFormat:@"Time To Train: %iD %iH %iM %iS", time.days, time.hours, time.minutes, time.seconds];
			
		}
		
		//Skill Points to next Level String
		
		NSString* skillPointsString;
		if(level == 5)
		{
			skillPointsString = [NSString stringWithString:@"Skill is at max level."];
		} else {
			int rank = learnedSkill.skill.rank.intValue;
			int skillPoints = learnedSkill.skillPointsLearned.intValue;
			int skillPointsToNextLevel = [EMSSkillTrainingInfo skillPointsRequiredForLevel:(level + 1) rank:rank] - skillPoints;
			skillPointsString = [NSString stringWithFormat:@"Skill points required to next level: %@.", 
								 [numberFormatter stringFromNumber:[NSNumber numberWithInt:skillPointsToNextLevel]]];
		}
		
		//Primary and secondary Attributes String
		NSString* primaryAttribute = learnedSkill.skill.primaryAttribute.capitalizedString;
		NSString* secondaryAttribute = learnedSkill.skill.secondaryAttribute.capitalizedString;
		NSString* attributesString = [NSString stringWithFormat:@"Primary: %@, Secondary: %@", primaryAttribute, secondaryAttribute];
		
		NSString* tooltipString = [NSString stringWithFormat:@"%@\n%@\n\n%@\n%@\n\n%@", 
								   skillName, skillDescription, skillPointsString, timeToTrainString, attributesString];
		return tooltipString;
	}
	else return nil;
}

-(float)outlineView:(NSOutlineView*)outlineView heightOfRowByItem:(id)item
{
	NSManagedObject* displayInfo = [item representedObject];
	if([[[displayInfo entity] name] isEqualToString:@"EVECDLearnedGroup"])
		return 17.0;
	else return 30.0;
}

-(NSArray*)positionSortDescriptors
{
	return [EMSGeneralUtils positionSortDescriptors];
}

-(NSArray*)nameSortDescriptors
{
	return [EMSGeneralUtils nameSortDescriptors];
}

-(EMSRange)queueStatusForCell:(id)aCell
{
	EVECDQueueItem* cellQueueItem = (EVECDQueueItem*)[EVECDContext objectWithID:[aCell objectValue]];
	NSArray* queuedItems = [queuedSkillsArrayController arrangedObjects];
	double positionCounter = 0;
	EMSRange range;
	for(EVECDQueueItem* queueItem in queuedItems)
	{
		NSTimeInterval trainingTime = queueItem.trainingTime.doubleValue / 86400;
		if(queueItem == cellQueueItem)
		{
			range.location = positionCounter;
			range.length = trainingTime;
			return range;
		} else {
			positionCounter += trainingTime;
		}
	}
	return range;
}

@end
